# EDXLSharp AWS Router Install Script
# Copyright 2014 The MITRE Corporation
# Licensed under the Apache 2.0 Licensed
# Don McGarry

# This script is designed to be run on an AWS instance with the proper IAM role permissions for SQS and Dynamo
# Using roles means that we don't need to store credentials in the script which is a good thing
# This script uses AWS Tools for Powershell which is installed on all AWS Windows AMIs by default
# To use, set the variables below and run the script

# Required user variables:
# The AWS Region to create resources in
$AWSRegion = "us-east-1"
# Your AWS Account Number (this is used to dynamically generate ARNs and Policies)
$AWSAcct = "122017571978"
# The name of the role for your EC2 servers to access the SQS queues
$RoleName = "icnet-dev"
# Read Capacity Units for EDXL Cache
$EDXLRead = 1
# Write  Capacity Units for EDXL Cache
$EDXLWrite = 1
# Read Capacity Units for Content Cache
$ContentRead = 1
# Write  Capacity Units for Content Cache
$ContentWrite = 1
# Read Capacity Units for Link Cache
$LinkRead = 1
# Write  Capacity Units for Link Cache
$LinkWrite = 1
# Read Capacity Units for ValueLists
$VLRead = 1
# Write  Capacity Units for ValueLists
$VLWrite = 1
# Read Capacity Units for Rules
$RulesRead = 1
# Write  Capacity Units for Rules
$RulesWrite = 1
# Read Capacity Units for Rules
$HistoryRead = 1
# Write  Capacity Units for Rules
$HistoryWrite = 1


# Constants (You could change these, but why?)
$DEProcessingSQSQueueName = "icnet-deprocessing"
$ContentProcessingSQSQueueName = "icnet-contentprocessing"
$ContentFeedProcessingSQSQueueName = "icnet-contentfeedprocessing"
$FederationProcessingSQSQueueName = "icnet-federationprocessing"
$HistoryProcessingSQSQueueName = "icnet-historyprocessing"
$EDXLDynamoTableName = "EDXLCache"
$DynamoContentTableName = "ContentCache"
$DynamoFeedTableName = "FeedCache"
$ValueListDynamoTableName = "ValueLists"
$RulesDynamoTableName = "Rules"
$HistoryDynamoTableTame = "EDXLHistory"

# Begin demz code
Import-Module AWSPowerShell
Set-DefaultAWSRegion $AWSRegion
$sqspolicyjson=@"
{
	"Version": "2008-10-17",
	"Id": "/$AWSAcct/icnet-deprocessing/SQSDefaultPolicy",
	"Statement": [
		{
			"Sid": "Sid8675309311592",
			"Effect": "Allow",
			"Principal": {
				"AWS": "arn:aws:iam::${AWSAcct}:role/$RoleName"
			},
			"Action": "SQS:*",
			"Resource": "arn:aws:sqs:${AWSRegion}:${AWSAcct}:icnet-deprocessing"
		}
	]
}
"@
Write-Host $sqspolicyjson
$sqspolicyjson=$sqspolicyjson -replace "`t|`n|`r",""
$sqspolicyjson=$sqspolicyjson -replace " ",""
Write-Host $sqspolicyjson
$sqsopts = @{"ReceiveMessageWaitTimeSeconds" = "20"; "VisibilityTimeout" = "0"}
$sqsurl=New-SQSQueue -QueueName $DEProcessingSQSQueueName -Attributes $sqsopts
Write-Host $sqsurl
$sqsopts = @{"Policy" = $sqspolicyjson}
Set-SQSQueueAttribute -QueueUrl $sqsurl -Attributes $sqsopts
$sqspolicyjson=$sqspolicyjson -replace $DEProcessingSQSQueueName, $ContentProcessingSQSQueueName
Write-Host $sqspolicyjson
$sqsurl=New-SQSQueue -QueueName $ContentProcessingSQSQueueName -Attributes $sqsopts
Write-Host $sqsurl
$sqsopts = @{"Policy" = $sqspolicyjson}
Set-SQSQueueAttribute -QueueUrl $sqsurl -Attributes $sqsopts
$sqspolicyjson=$sqspolicyjson -replace $ContentProcessingSQSQueueName, $ContentFeedProcessingSQSQueueName
Write-Host $sqspolicyjson
$sqsurl=New-SQSQueue -QueueName $ContentFeedProcessingSQSQueueName -Attributes $sqsopts
Write-Host $sqsurl
$sqsopts = @{"Policy" = $sqspolicyjson}
Set-SQSQueueAttribute -QueueUrl $sqsurl -Attributes $sqsopts
$sqspolicyjson=$sqspolicyjson -replace $ContentFeedProcessingSQSQueueName, $FederationProcessingSQSQueueName
Write-Host $sqspolicyjson
$sqsurl=New-SQSQueue -QueueName $FederationProcessingSQSQueueName -Attributes $sqsopts
Write-Host $sqsurl
$sqsopts = @{"Policy" = $sqspolicyjson}
Set-SQSQueueAttribute -QueueUrl $sqsurl -Attributes $sqsopts
$sqspolicyjson=$sqspolicyjson -replace $FederationProcessingSQSQueueName, $HistoryProcessingSQSQueueName
Write-Host $sqspolicyjson
$sqsurl=New-SQSQueue -QueueName $HistoryProcessingSQSQueueName -Attributes $sqsopts
Write-Host $sqsurl
$sqsopts = @{"Policy" = $sqspolicyjson}
Set-SQSQueueAttribute -QueueUrl $sqsurl -Attributes $sqsopts

$tables = Get-DDBTables
Write-Host $tables
$found = 0
foreach ($table in $tables)
{
	if ($table -eq $EDXLDynamoTableName)
	{
		$found = 1
		break
	}
}

if($found -eq 1)
{
	Write-Host "EDXL Dynamo Table Already Exists"
}
else
{
	Write-Host "Creating EDXL DynamoDB Table"
	$Schema=New-DDBTableSchema
	$Schema | Add-DDBKeySchema -KeyDataType "S" -KeyName "distributionID" -KeyType "HASH"
	$Schema | Add-DDBKeySchema -KeyDataType "S" -KeyName "senderID" -KeyType "RANGE"
	New-DDBTable -TableName $EDXLDynamoTableName -Schema $Schema -ReadCapacity $EDXLRead -WriteCapacity $EDXLWrite
}

$found = 0
foreach ($table in $tables)
{
	if ($table -eq $DynamoContentTableName)
	{
		$found = 1
		break
	}
}

if($found -eq 1)
{
	Write-Host "Content Dynamo Table Already Exists"
}
else
{
	Write-Host "Creating Content DynamoDB Table"
	$Schema=New-DDBTableSchema
	$Schema | Add-DDBKeySchema -KeyDataType "N" -KeyName "_ContentHash" -KeyType "HASH"
	New-DDBTable -TableName $DynamoContentTableName -Schema $Schema -ReadCapacity $ContentRead -WriteCapacity $ContentWrite
}


$found = 0
foreach ($table in $tables)
{
	if ($table -eq $DynamoFeedTableName)
	{
		$found = 1
		break
	}
}

if($found -eq 1)
{
	Write-Host "Feed Dynamo Table Already Exists"
}
else
{
	Write-Host "Creating Feed DynamoDB Table"
	$Schema=New-DDBTableSchema
	$Schema | Add-DDBKeySchema -KeyDataType "S" -KeyName "_FeedHash" -KeyType "HASH"
	#$Schema | Add-DDBKeySchema -KeyDataType "S" -KeyName "_ContentHash" -KeyType "RANGE"
	New-DDBTable -TableName $DynamoContentTableName -Schema $Schema -ReadCapacity $FeedRead -WriteCapacity $FeedWrite
}

$found = 0
foreach ($table in $tables)
{
	if ($table -eq $ValueListDynamoTableName)
	{
		$found = 1
		break
	}
}
if($found -eq 1)
{
	Write-Host "ValueLists Dynamo Table Already Exists"
}
else
{
	Write-Host "Creating ValueLists DynamoDB Table"
	$Schema=New-DDBTableSchema
	$Schema | Add-DDBKeySchema -KeyDataType "S" -KeyName "valueListUrn" -KeyType "HASH"
	New-DDBTable -TableName $ValueListDynamoTableName -Schema $Schema -ReadCapacity $VLRead -WriteCapacity $VLWrite
}

$found = 0
foreach ($table in $tables)
{
	if ($table -eq $RulesDynamoTableName)
	{
		$found = 1
		break
	}
}
if($found -eq 1)
{
	Write-Host "Rules Dynamo Table Already Exists"
}
else
{
	Write-Host "Creating Rules DynamoDB Table"
	$Schema=New-DDBTableSchema
	$Schema | Add-DDBKeySchema -KeyDataType "S" -KeyName "valueListUrn" -KeyType "HASH"
	$Schema | Add-DDBKeySchema -KeyDataType "S" -KeyName "value" -KeyType "RANGE"
	New-DDBTable -TableName $RulesDynamoTableName -Schema $Schema -ReadCapacity $RulesRead -WriteCapacity $RulesWrite
}

$found = 0
foreach ($table in $tables)
{
	if ($table -eq $HistoryDynamoTableName)
	{
		$found = 1
		break
	}
}
if($found -eq 1)
{
	Write-Host "History Dynamo Table Already Exists"
}
else
{
	Write-Host "Creating History DynamoDB Table"
	$Schema=New-DDBTableSchema
	$Schema | Add-DDBKeySchema -KeyDataType "S" -KeyName "distributionID" -KeyType "HASH"
	$Schema | Add-DDBKeySchema -KeyDataType "S" -KeyName "_ReceivedTime" -KeyType "RANGE"
	New-DDBTable -TableName $HistoryDynamoTableName -Schema $Schema -ReadCapacity $HistoryRead -WriteCapacity $HistoryWrite
}